package com.yingside.collection.game;

import java.util.Scanner;

public class Game {

    private Hero h;
    private Store s;
    private String space = "\t\t\t\t";
    private String sider = "\t======================================================================";

    private Scanner input = new Scanner(System.in);

    public Game(){
        init();
        menu();
    }

    public void init(){
        System.out.println("...正在初始化英雄...");
        h = new Hero("孙悟空");
        System.out.println("...正在初始化商店...");
        s = new Store();
    }

    public void menu(){
        System.out.println(sider);
        System.out.println(space + "1.查看状态");
        System.out.println(space + "2.逛商店");
        System.out.println(space + "3.装备管理");
        System.out.println(space + "4.打怪赚钱");
        System.out.println(space + "5.酒馆休息");
        System.out.println(space + "0.退出");

        System.out.println(sider);

        System.out.println(space + "请选择(0-5):");
        int n = input.nextInt();

        switch(n){
            case 1:
                showStatus();
                break;
            case 2:
                store();
                break;
            case 3:
                equipmentManager();
                break;
            case 4:
                fight();
                break;
            case 5:
                break;
            case 0:
                break;
        }
    }

    public void equipmentManager(){
        int index = 1;
        //先显示身上已经装备了哪些武器
        System.out.println(sider);
        showEquipment();
        System.out.println(sider);

        //显示英雄背包中有哪些内容
        System.out.println(sider);
        //由于装备很多,要一下子全部展示出来,就全部一起编号显示
        System.out.println("\t序号\t名称\t功效\t");

        //首先循环武器背包
        for (int i = 0; i < h.getWeaponBag().size(); i++) {
            Weapon w = h.getWeaponBag().get(i);

            System.out.println("\t" + (index ++) + "\t"
                    + w.getName() + "\t"
                    + "攻击:" + w.getAttack() + "\t");
        }

        //循环护甲背包
        for (int i = 0; i < h.getArmorBag().size(); i++) {
            Armor a = h.getArmorBag().get(i);

            System.out.println("\t" + (index ++) + "\t"
                    + a.getName() + "\t"
                    + "攻击:" + a.getDefence() + "\t");
        }

        //接下来依次循环鞋子和盾牌,那么index的值就应该依次是1,2,3,4.....9,10;
        System.out.println(sider);


        if(index == 1){
            System.err.println("\t你的背包空空如也~~~先逛一下商店吧~~~");
        }
        else{
            System.out.println("\t请输入你要装备的序号(1-"+(index-1)+"),按0返回主菜单");
        }

        int n = input.nextInt();

        if(n == 0){
            menu();
        }
        else if(index != -1 && n >= 1 && n<=index-1){

            //如果输入的n的范围在武器背包的长度范围之内,那就应该是装备的武器

            if(n <= h.getWeaponBag().size()){
                //找到背包中的武器
                Weapon w = h.getWeaponBag().get(n-1);
                h.dressEquipment(w);
                equipmentManager();
            }
            //如果输入n在护甲范围之内,那就装备护甲
//			else if(n <= h.getWeaponBag().size() + h.getArmorBag().size()){
//
//			}


        }
        else{
            System.err.println("请输入正确的序号~~~");
            equipmentManager();
        }



    }

    public void showEquipment(){
        //显示英雄身上的装备
        System.out.println("英雄当前装备:\n");
        System.out.print("\t武器:");
        if(h.getEquipment().getWeapon() == null){
            System.out.println("无");
        }else{
            System.out.println(h.getEquipment().getWeapon().getName() + ",攻击力:" + h.getEquipment().getWeapon().getAttack());
        }

        System.out.print("\t护甲:");
        if(h.getEquipment().getArmor() == null){
            System.out.println("无");
        }else{}

        System.out.print("\t鞋子:");
        if(h.getEquipment().getShoes() == null){
            System.out.println("无");
        }else{}

        System.out.print("\t盾牌:");
        if(h.getEquipment().getShield() == null){
            System.out.println("无");
        }else{}


    }

    public void buyWeapon(){
        System.out.println(space + "序号\t名称\t功效\t价格\t库存\t");
        System.out.println(sider);
        for (int i = 0; i < s.getWeapons().size(); i++) {
            Weapon w = s.getWeapons().get(i);
            System.out.println(space + (i+1) + "\t"
                    + w.getName() + "\t"
                    + w.getAttack() + "\t"
                    + w.getPrice() + "\t"
                    + w.getNum() + "\t");

        }
        System.out.println(sider);

        System.out.println(space + "你现在拥有的金钱是:" + h.getOwnMoney());
        System.out.println(space + "请选择你要购买的武器(1-"+ s.getWeapons().size() + "),输入0返回上级菜单:");
        int n = input.nextInt();

        if(n == 0){
            menu();
        }
        else if(n >= 1 && n <= s.getWeapons().size()){
            //如果输入正确,英雄需要到商店购买武器,实际就是商店卖武器
            s.sell(h,s.getWeapons().get(n-1));
            buyWeapon();
        }
        else{
            System.err.println("\n\t\t输入错误");

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            buyWeapon();
        }

    }


    public void store(){
        System.out.println(sider);

        System.out.println(space + "1.武器");
        System.out.println(space + "2.护甲");
        System.out.println(space + "3.鞋子");
        System.out.println(space + "4.盾牌");
        System.out.println(space + "0.返回主菜单");
        System.out.println(sider);
        System.out.println(space + "请选择(0-4):");
        int n = input.nextInt();
        switch(n){
            case 1:
                buyWeapon();
                break;
        }
    }

    public void showStatus(){
        System.out.println(space + "英雄状态:");
        System.out.println(sider);
        System.out.println(space + "姓名:" + h.getName());
        System.out.println(space + "攻击:" + h.getAttack());
        System.out.println(space + "防御:" + h.getDefence());
        System.out.println(space + "血量:" + h.getHp() + "/100");
        System.out.println(space + "金币:" + h.getOwnMoney());
        System.out.println(sider);

        menu();
    }


    public Enemy getRandomEnemy(){
        Enemy e = null;

        int r = GameUtil.myRandom(1, 2);
        int t = GameUtil.myRandom(1, 4);

        switch(r){
            case 1:
                e = new Monster(t);
                break;
            case 2:
                e = new Vampire(t);
                break;
        }

        return e;
    }

    public void fight(){
        //增加游戏敌人的随机性
        Enemy e = getRandomEnemy();

        System.out.println(space + "=========怪物出现============");
        System.out.println(space + "====" + h.getName() + " V  S  " + e.getName() + "=========");

        String token = "hero";

        while(true){
            if(token.equals("hero")){

                h.fight(e);

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                token = "enemy";
            }
            else if(token.equals("enemy")){


                e.response(h);

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                token = "hero";
            }


            if(h.getHp() <= 0){
                System.err.println(space + "胜败乃兵家常事,少侠请重新再来");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                System.out.println(space + "突然的圣光沐浴了你...你的血量回复到了1...");

                h.setHp(1);

                break;
            }
            else if(e.getHp() <= 0){
                System.out.println("英雄胜利,怪兽死亡...");
                System.out.println("你获得了:" + e.getMoney() + "的金币~~~");
                h.setOwnMoney(h.getOwnMoney() + e.getMoney());
                break;
            }
        }

        menu();

    }

}

